#coding=utf-8
import json

# import xlwt

from django.http import HttpResponse


# Create your views here.
from apps.rm import cmRiskWhite

from tools import httpUtil


def cm_risk_white(request, param):

    if request.method == 'POST':
        form = request.POST
    elif request.method == 'GET':
        form = request.GET

    if param == 'init':
        res = cmRiskWhite.init()
        return HttpResponse(json.dumps(res))

    elif param == 'save':
        invest_id = form.get('formobj.account_id')
        stock_code = form.get('formobj.stock_code')
        rule_id = form.get('formobj.rule_id')

        if rule_id == '':
            cmRiskWhite.add(invest_id, stock_code)
        else :
            cmRiskWhite.modify(invest_id, stock_code, rule_id)

        return HttpResponse(True)

    elif param == 'add':
        invest_id = form.get('formobj.account_id')
        stock_code = form.get('formobj.stock_code')
        rule_id = form.get('formobj.rule_id')

        cmRiskWhite.add(invest_id, stock_code)

    elif param == 'delete':
        rule_id = form.get('rule_id')
        cmRiskWhite.delete(rule_id)
        return HttpResponse(True)

    elif param == 'modify':
        invest_id = form.get('invest_id')
        stock_code = form.get('stock_code')
        rule_id = form.get('rule_id')
        cmRiskWhite.modify(invest_id, stock_code, rule_id)

    elif param == 'query':
        invest_id = form.get('invest_id')
        stock_code = form.get('stock_code')
        rule_id = form.get('rule_id')
        res = cmRiskWhite.query(invest_id, stock_code, rule_id) # [(invest_id, 'stock_code', rule_id),(...)...]

        # print invest_id, stock_code, res

        rows = []
        for i in res:
            str_format = "{'rule_id':'%d', " \
                         "'invest_account_id':'%d', " \
                         "'invest_account_name':'%s'," \
                         "'stock_code':'%s'}"%(i[2],i[0],'name',i[1])
            rows.append(eval(str_format))

        res = {'total': str(len(rows)), 'rows' : rows}
        return HttpResponse(json.dumps(res))

    elif param == 'export_excel':
        wb = cmRiskWhite.export_excel()
        return wb

    elif param == 'file_upload':
        return cmRiskWhite.file_upload(request)


    elif param == 'file_download':
        # do something...
        '''此处是使用django框架下的邮件系统进行email的发送'''
        from django.core.mail import send_mail,EmailMessage
        import os
        BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
        the_file_name = BASE_DIR + "/upload/"
        attachname = the_file_name + "a.txt"


        em = EmailMessage('subject','body','chengk@chinaetf.com.cn',to=['463241800@qq.com'])
        em.attach_file(attachname)
        em.send()
        # send_mail('subject', 'this is the message of email', "zhengyb@chinaetf.com.cn", ['575393442@qq.com',], fail_silently=True)
        return cmRiskWhite.file_download(request)

    elif param == '':
        #return render(request, 'rm/cmRiskWhite_bak.html')
        #return mymako.hello(request)
        #print request
        #return httpUtil.mako_render(request,'/rm/hello.html',{'name':'mm'})
        return httpUtil.mako_render(request,'/bm/hello.html',{'name':'mm'})
        #return mymako.mako_render(request,'/rm/cmRiskWhite_bak.html',{'name':'mm'})
        #return mymako.hello(request)

def paradeal(request, model, action, method):
    print 'dealed' , model, action , method

    return HttpResponse('hello !')

if __name__ == '__main__':
    res = cmRiskWhite.query(2, '9090')
    print res


